home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / tool chest / development kits / hypercard related / xcmds & xfcns / byrne's xcmds&xfcns / source / tracktoabs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-01  |  5.8 KB  |  224 lines

  1. /*
  2.     TrackToAbs XFCN v1.0
  3.     
  4.     ©1991 Apple Computer, Inc.; by Mike Byrne
  5.     
  6.     Takes a track number and a time in that track and converts it to the absolute time on
  7.     the disc.  Note that the CD-Audio XCMDs *must* be present, and a CD must be in the drive...
  8.     
  9.     Form:
  10.     TrackToAbs(<track#>, <time>)
  11.     
  12.     # the MPW 3.2 build commands:
  13.     C -b TrackToAbs.c -mbg off
  14.         Link -w -t STAK -c WILD -rt XFCN=703 ∂
  15.             -m ENTRYPOINT ∂
  16.             -sg TrackToAbs ∂
  17.             TrackToAbs.c.o ∂
  18.             "{Libraries}HyperXLib.o" ∂
  19.             "{Libraries}Runtime.o" ∂
  20.             "{Libraries}Interface.o" ∂
  21.             "{CLibraries}StdCLib.o" ∂
  22.             -o "teststack"
  23. */
  24.  
  25. #include <Types.h>
  26. #include <string.h>
  27. #include <Memory.h>
  28. #include "HyperXcmd.h"
  29.  
  30. #define NULL (long) 0
  31. #define NIL (long) 0
  32.  
  33. #define kNumParams     2
  34. #define    blocksInSec 75
  35. #define secsInMin    60
  36.  
  37.  
  38. /* prototypes */
  39. void ErrorBack(XCmdPtr paramPtr, char *message);
  40. void MoveLockParams ( XCmdPtr paramPtr, short paramCount );
  41. void UnlockParams  ( XCmdPtr paramPtr, short paramCount );
  42. void StringToInts( char* theString, long* mint, long* sint, long* bint);
  43. void IntsToString(char* retString, long mint, long sint, long bint);
  44.  
  45. pascal void EntryPoint(XCmdPtr paramPtr)
  46. {
  47.  
  48.     /* variable declarations */
  49.     long    mint1, sint1, bint1;
  50.     long    mint2, sint2, bint2;
  51.     long    mintr, sintr, bintr;
  52.     long    carry = 0;
  53.     char    retString[10];
  54.     char    msgString[40];
  55.     Handle    dummyHandle;
  56.  
  57.     /* move high and lock the parameters. */
  58.     MoveLockParams(paramPtr, paramPtr->paramCount);
  59.  
  60.     /* check for copyright or syntax help request */
  61.     if (!strcmp( (char*)*paramPtr->params[0], "!") ) {
  62.         ErrorBack(paramPtr, "v1.0, ©1991 Apple Computer, Inc.; by Mike Byrne");
  63.         UnlockParams(paramPtr, paramPtr->paramCount);
  64.         return;
  65.     } else if (!strcmp ( (char*)*paramPtr->params[0], "?") ) {
  66.         ErrorBack(paramPtr, "TrackToAbs syntax is 'TrackToAbs(<track#>, <time>)'");
  67.         UnlockParams(paramPtr, paramPtr->paramCount);
  68.         return;
  69.     }
  70.  
  71.     /* not a copyright or help request.       */     
  72.     /* check for correct number of parameters */
  73.     if (paramPtr->paramCount != kNumParams) {
  74.         ErrorBack(paramPtr, "Error:  TrackToAbs syntax is 'TrackToAbs(<track#>, <time>)'");
  75.         UnlockParams(paramPtr, paramPtr->paramCount);
  76.         return;
  77.     }
  78.     
  79.     /* verify that there is a CD in there */
  80.     dummyHandle = GetGlobal(paramPtr, "\pxxxCDRefNum");
  81.     if ( !strcmp( (char*)*dummyHandle, "0") ) {
  82.         ErrorBack(paramPtr, "Error: There is no audio CD available");
  83.         UnlockParams(paramPtr, paramPtr->paramCount);
  84.         DisposHandle(dummyHandle);
  85.         return;
  86.     }
  87.     DisposHandle(dummyHandle);
  88.     
  89.     /* get the start time */
  90.     strcpy( msgString, "line ");
  91.     strcat( msgString, (char*) *paramPtr->params[0]);
  92.     strcat( msgString, " of CDTOCLines()" );
  93.     c2pstr(msgString);
  94.     dummyHandle = EvalExpr(paramPtr, msgString);
  95.     
  96.     /* check to see if we got anything in the string */
  97.     if (strlen( (char*) *dummyHandle ) < 1) {
  98.         ErrorBack(paramPtr, "Error:  That is not a valid track for this disc.");
  99.         UnlockParams(paramPtr, paramPtr->paramCount);
  100.         DisposHandle(dummyHandle);
  101.         return;
  102.     }        
  103.     
  104.     /* check to see if the first char in the string is a "-" (an error) */
  105.     if ( (*dummyHandle)[0] == '-' ) {
  106.         ErrorBack(paramPtr, "Error:  Could not get disc information.");
  107.         UnlockParams(paramPtr, paramPtr->paramCount);
  108.         DisposHandle(dummyHandle);
  109.         return;
  110.     }        
  111.         
  112.  
  113.     /* convert the times to integers  */
  114.     StringToInts( (char*)*dummyHandle, &mint1, &sint1, &bint1);
  115.     StringToInts( (char*)*paramPtr->params[1], &mint2, &sint2, &bint2);
  116.  
  117.     /* add them, with carrying */
  118.     carry = (bint1 + bint2) / blocksInSec;
  119.     bintr = (bint1 + bint2) % blocksInSec;
  120.     sintr = sint1 + sint2 + carry;
  121.     carry = sintr / secsInMin;
  122.     sintr = sintr % secsInMin;
  123.     mintr = mint1 + mint2 + carry;
  124.     
  125.     /* convert the numbers back to a string and go home */
  126.     IntsToString(retString, mintr, sintr, bintr);
  127.     DisposHandle(dummyHandle);
  128.     ErrorBack(paramPtr, retString);
  129.     UnlockParams(paramPtr, paramPtr->paramCount);
  130.     return;
  131.     
  132. }
  133.  
  134.  
  135.  
  136. /* convert a string of the form "mm,ss,bb" to three shorts 
  137.    ------------------------------------------------------- */
  138. void StringToInts( char* theString, long* mint, long* sint, long* bint)
  139. {
  140.     char*    mptr;
  141.     char*    sptr;
  142.     char*    bptr;
  143.     char    mstr[5], sstr[5], bstr[5];
  144.     
  145.     mptr = strtok(theString, ",");
  146.     sptr = strtok(NULL, ",");
  147.     bptr = strtok(NULL, ",");
  148.     strcpy(mstr, mptr);
  149.     strcpy(sstr, sptr);
  150.     strcpy(bstr, bptr);
  151.     c2pstr(mstr);
  152.     c2pstr(sstr);
  153.     c2pstr(bstr);
  154.     StringToNum(mstr, mint);
  155.     StringToNum(sstr, sint);
  156.     StringToNum(bstr, bint);
  157. }
  158.  
  159.  
  160.  
  161. /* convert three integers to a c string of the form "mm,ss,bb" 
  162.    ----------------------------------------------------------- */
  163. void IntsToString(char* retString, long mint, long sint, long bint)
  164. {
  165.     Str255     mstr, sstr, bstr;
  166.     
  167.     NumToString(mint, mstr);
  168.     NumToString(sint, sstr);
  169.     NumToString(bint, bstr);
  170.     p2cstr(mstr);
  171.     p2cstr(sstr);
  172.     p2cstr(bstr);
  173.     strcpy(retString, mstr);
  174.     strcat(retString, ",");
  175.     strcat(retString, sstr);
  176.     strcat(retString, ",");
  177.     strcat(retString, bstr);
  178. }
  179.  
  180.     
  181. /* allocate and load up paramPtr->returnValue with a string 
  182.    -------------------------------------------------------- */
  183. void ErrorBack(XCmdPtr paramPtr, char *message)
  184. {
  185.     Handle  mesHnd;
  186.  
  187.     /*
  188.         Allocate space for an error message.
  189.         Copy the string into it.
  190.         Return the handle to HyperCard.
  191.     */
  192.     mesHnd = NewHandle((long)(strlen(message)+1));
  193.     if (mesHnd == nil) return;
  194.     strcpy((char *)*mesHnd,message);
  195.     paramPtr->returnValue = mesHnd;
  196. }
  197.  
  198.  
  199.  
  200. /*  move high and lock down all parameters  
  201.     ----------------------------------------------------------------------- */
  202. void MoveLockParams ( XCmdPtr paramPtr, short paramCount )
  203. {
  204.     short i;
  205.     
  206.     for(i=0; i <= paramCount-1; i++)
  207.     {
  208.         MoveHHi(paramPtr->params[i]);
  209.         HLock(paramPtr->params[i]);
  210.     }
  211. }
  212.  
  213.  
  214.  
  215.  
  216. /* unlock all parameter handles in the XFCNBlock  
  217.    ---------------------------------------------  */
  218. void UnlockParams  ( XCmdPtr paramPtr, short paramCount )
  219. {    short i;
  220.     
  221.     for(i=0; i <= paramCount-1; i++)
  222.         { HUnlock(paramPtr->params[i]);}
  223. }
  224.